package com.tengju.bff.interfaces.manage.crm.dealer.facade;

import com.tengju.bff.interfaces.manage.crm.dealer.assembler.CrmDealerVOAssembler;
import com.tengju.bff.interfaces.manage.crm.dealer.form.CrmDealerInfoExportForm;
import com.tengju.bff.interfaces.manage.crm.dealer.form.CrmDealerInfoQueryForm;
import com.tengju.bff.interfaces.manage.crm.dealer.vo.*;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.PageResultVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfo;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfoRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: kezunfa
 * @Date: 2021/4/22 13:13
 * @Description:
 */
@Service
public class CrmDealerInfoFacade {

    @Autowired
    private CrmDealerInfoRepository crmDealerInfoRepository;


    /**
     * 获取经销商
     *
     * @param crmDealerInfoQueryForm 经销商分页查询参数
     * @return
     */
    public TableVO queryCrmDealerInfos(CrmDealerInfoQueryForm crmDealerInfoQueryForm) {
        TableVO table = new TableVO();
        table.setHeader(List.of(
                ColumnVO.of("idCode", "会员id", false),
                ColumnVO.of("userName", "姓名", false),
                ColumnVO.of("nickName", "昵称", false),
//                ColumnVO.of("weChat", "微信号", false),
//                ColumnVO.of("goodNumber", "靓号", false),
                ColumnVO.of("inviteCode", "邀请码", false),
                ColumnVO.of("level", "等级", false),
                ColumnVO.of("applyType", "升级方式", false),
                ColumnVO.of("starLevel", "服务商等级", false),
                ColumnVO.of("userCountry", "国家", false),
                ColumnVO.of("userProvince", "省份", false),
                ColumnVO.of("userCity", "城市", false),
                ColumnVO.of("dealerSupInfoVO", "上级信息", false),
                ColumnVO.of("dealerAreaInfoVO", "大区域数据", false),
                ColumnVO.of("dealerCustInfoVO", "直属客户", false),
                ColumnVO.of("levelChgAt", "升级时间", false)
        ));
        List<Map<String, Object>> rows = new ArrayList<>();
        table.setRecords(rows);
        // 设置分页
        table.setPage(PageResultVO.of(crmDealerInfoQueryForm.getPageIndex(),
                crmDealerInfoQueryForm.getPageSize(), 0));

        List<UserIdCode> genSecIdCodeList = ServletContextUtil.getServletContext().getGenSecIdCodes();
        if (CollectionUtils.isEmpty(genSecIdCodeList)) {
            return table;
        }
        List<Long> genSecIdCodeValList = genSecIdCodeList.stream()
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());
        List<CrmDealerInfo> crmDealerInfoList = crmDealerInfoRepository.getCrmDealerInfoList(crmDealerInfoQueryForm.getIdCode(),
                crmDealerInfoQueryForm.getLevel(),
                crmDealerInfoQueryForm.getStartTime(),
                crmDealerInfoQueryForm.getEndTime(),
                crmDealerInfoQueryForm.getAreaSupIdCode(),
                genSecIdCodeValList,
                crmDealerInfoQueryForm.getInviteCode(),
                crmDealerInfoQueryForm.getGoodNumber(),
                crmDealerInfoQueryForm.getWeChat(),
                crmDealerInfoQueryForm.getMobile(),
                crmDealerInfoQueryForm.getOffset(),
                crmDealerInfoQueryForm.getPageSize());
        List<CrmDealerInfoVO> crmDealerInfoVOList = CrmDealerVOAssembler.crmDealerInfoListToVOList(crmDealerInfoList);

        int count = crmDealerInfoRepository.getCrmDealerInfoCount(
                crmDealerInfoQueryForm.getIdCode(),
                crmDealerInfoQueryForm.getLevel(),
                crmDealerInfoQueryForm.getStartTime(),
                crmDealerInfoQueryForm.getEndTime(),
                crmDealerInfoQueryForm.getAreaSupIdCode(),
                genSecIdCodeValList,
                crmDealerInfoQueryForm.getInviteCode(),
                crmDealerInfoQueryForm.getGoodNumber(),
                crmDealerInfoQueryForm.getWeChat(),
                crmDealerInfoQueryForm.getMobile());

        // 设置分页
        table.setPage(PageResultVO.of(crmDealerInfoQueryForm.getPageIndex(), crmDealerInfoQueryForm.getPageSize(), count));

        for (CrmDealerInfoVO crmVO : crmDealerInfoVOList) {
            Map<String, Object> row = new HashMap<>();
            row.put("idCode", crmVO.getIdCode());
//            row.put("weChat", crmVO.getWeChat());
            row.put("inviteCode", crmVO.getInviteCode());
//            row.put("goodNumber", crmVO.getGoodNumber());
            row.put("userName", crmVO.getUserName());
            row.put("nickName", crmVO.getNickName());
            row.put("level", crmVO.getLevel());
            row.put("applyType", crmVO.getApplyType());
            row.put("starLevel", crmVO.getStarLevel());
            row.put("userCountry", crmVO.getUserCountry());
            row.put("userProvince", crmVO.getUserProvince());
            row.put("userCity", crmVO.getUserCity());
            row.put("dealerSupInfoVO", crmVO.getDealerSupInfoVO());
            row.put("dealerAreaInfoVO", crmVO.getDealerAreaInfoVO());
            row.put("dealerCustInfoVO", crmVO.getDealerCustInfoVO());
            row.put("levelChgAt", crmVO.getLevelChgAt());
            rows.add(row);
        }
        // 设置行数据
        table.setRecords(rows);
        return table;

    }

    /**
     * @return
     * @author kezunfa
     * @date 2021/4/23 13:50
     */
    public List<ExportCrmDealerInfoVO> exportCrmDealerInfoVOList(CrmDealerInfoExportForm crmDealerInfoExportForm) {
        List<CrmDealerInfo> crmDealerInfos = new ArrayList<>();

        List<UserIdCode> genSecIdCodeList = crmDealerInfoExportForm.getGenSecIdCodes();
        if (CollectionUtils.isEmpty(genSecIdCodeList)) {
            return new ArrayList<>();
        }
        List<Long> collect = genSecIdCodeList.stream()
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());
        int limit = 500;//数据条数
        for (int i = 0; i < 1000000; i++) {
            List<CrmDealerInfo> tmp = crmDealerInfoRepository.getCrmDealerInfoList(
                    crmDealerInfoExportForm.getIdCode(),
                    crmDealerInfoExportForm.getLevel(),
                    crmDealerInfoExportForm.getStartTime(),
                    crmDealerInfoExportForm.getEndTime(),
                    crmDealerInfoExportForm.getAreaSupIdCode(),
                    collect,
                    crmDealerInfoExportForm.getInviteCode(),
                    crmDealerInfoExportForm.getGoodNumber(),
                    crmDealerInfoExportForm.getWeChat(),
                    crmDealerInfoExportForm.getMobile(),
                    i * limit,limit);
            if (CollectionUtils.isEmpty(tmp)){
                break;
            }
            crmDealerInfos.addAll(tmp);
            if (tmp.size() < limit) {
                break;
            }
        }
        return CrmDealerVOAssembler.exportCrmDealerInfoListToVOList(crmDealerInfos);
    }

    public KVVO getCrmDealerSupInfo(Long idCode) {

        CrmDealerInfo crmDealerInfo = crmDealerInfoRepository.getCrmDealerSupInfo(new UserIdCode(idCode));
        DealerSupInfoVO dealerSupInfoVO = new DealerSupInfoVO();
        if (crmDealerInfo != null) {
            dealerSupInfoVO.setSupUserName(String.format("上级：%s%s",crmDealerInfo.getSupUserName(),
                    crmDealerInfo.getSupIdCode() == null ? "":"("+crmDealerInfo.getSupIdCode()+")"));
            dealerSupInfoVO.setAsscHkName(String.format("所属黑卡：%s%s",crmDealerInfo.getAsscHkName(),
                    crmDealerInfo.getAsscHkIdCode() == null ? "":"("+crmDealerInfo.getAsscHkIdCode()+")"));
            dealerSupInfoVO.setBlStar1Name(String.format("所属1星：%s%s",crmDealerInfo.getBlStar1Name(),
                    crmDealerInfo.getBlStar1IdCode() == null ? "":"("+crmDealerInfo.getBlStar1IdCode()+")"
            ));
            dealerSupInfoVO.setBlStar2Name(String.format("所属2星：%s%s",crmDealerInfo.getBlStar2Name(),
                    crmDealerInfo.getBlStar2IdCode() == null ? "":"("+crmDealerInfo.getBlStar2IdCode()+")"
            ));
            dealerSupInfoVO.setBlStar3Name(String.format("所属3星：%s%s",crmDealerInfo.getBlStar3Name(),
                    crmDealerInfo.getBlStar3IdCode() == null ? "":"("+crmDealerInfo.getBlStar3IdCode()+")"
            ));
            dealerSupInfoVO.setBlStar4Name(String.format("所属4星：%s%s",crmDealerInfo.getBlStar4Name(),
                    crmDealerInfo.getBlStar4IdCode() == null ? "":"("+crmDealerInfo.getBlStar4IdCode()+")"
            ));
            dealerSupInfoVO.setBlEcName(String.format("所属EC：%s%s",crmDealerInfo.getBlEcName(),
                    crmDealerInfo.getBlEcIdCode() == null ? "":"("+crmDealerInfo.getBlEcIdCode()+")"
            ));
            dealerSupInfoVO.setBlSecName(String.format("所属SEC：%s%s",crmDealerInfo.getBlSecName(),
                    crmDealerInfo.getBlSecIdCode() == null ? "":"("+crmDealerInfo.getBlSecIdCode()+")"
            ));
            dealerSupInfoVO.setBlClubName(String.format("所属俱乐部：%s",crmDealerInfo.getBlClubName()));
        }

        return KVVO.of("所属上级信息",crmDealerInfo == null ? null : dealerSupInfoVO);
    }


}
